package com.seanLab.tool.TagBat;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.seanLab.domain.Article;
import com.seanLab.domain.TagInfo;
import com.seanLab.dto.SuggestArticleKeywordsDto;
import com.seanLab.dto.SuggestModelArticleDto;
import com.seanLab.dto.SuggestModelImageDto;
import com.seanLab.tool.TagSuggestModel.StaticModelProperties;
import com.seanLab.tool.TagSuggestor;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Deprecated
public class TagThread extends Thread {
    public static String modelPath;
    private List<JsonObject> articlesJson;
    private HashMap<String, JsonObject> imagesIndex;
    private TagSuggestor tagSuggestor;

    //results
    public List<String> idsOfImages;
    public List<List<TagInfo>> tagsOfImages;
    public List<List<SuggestArticleKeywordsDto>> keywordsOfArticle;

    public List<JsonObject> articlesResult;
    public List<JsonObject> imagesResult;

    public TagThread(List<JsonObject> articlesJson, HashMap<String, JsonObject> imagesIndex) {
        this.articlesJson = articlesJson;
        this.imagesIndex = imagesIndex;
        tagSuggestor = new TagSuggestor();
        tagSuggestor.loadModel(modelPath);
        idsOfImages = new ArrayList<>();
        tagsOfImages = new ArrayList<>();
        keywordsOfArticle = new ArrayList<>();
    }

    private void buildResutls() {
//        articlesResult = new ArrayList<>();
        imagesResult = new ArrayList<>();
        Gson gson = new Gson();
        for (int i = 0; i < articlesJson.size(); i++) {
            articlesJson.get(i).add("keywords", gson.toJsonTree(keywordsOfArticle.get(i)));
        }
        articlesResult = articlesJson;
        for (int i = 0; i < idsOfImages.size(); i++) {
            JsonObject image = imagesIndex.get(idsOfImages.get(i));
            image.add("imgTagList", gson.toJsonTree(tagsOfImages.get(i)));
            imagesResult.add(image);
        }
    }

    public void run() {
        System.out.println(this.getName() + " started!");
        Gson gson = new Gson();
        int count = 0;
        for (JsonObject articleJson : articlesJson) {
            List<String> imageIdList = new ArrayList<>();
            List<SuggestModelImageDto> imageDtoList = new ArrayList<>();
            SuggestModelArticleDto articleDto;
            Article article = gson.fromJson(articleJson, Article.class);
            for (String imgId : article.getImgID()) {
                JsonObject image = imagesIndex.get(imgId);
//                Image image = gson.fromJson(imagesIndex.get(imgId), Image.class);
                if (image != null) {
//                    imageDtoList.add(new SuggestModelImageDto(image.getOffset(), image.getImgDescription(), image.getContextAbove(), image.getContextBelow()));
                    try {
                        imageDtoList.add(new SuggestModelImageDto(image.get("offset").getAsInt(), image.get("imgDescription").getAsString(),
                                image.get("contextAbove").getAsString(), image.get("contextBelow").getAsString()));
                        imageIdList.add(imgId);
                    } catch (NullPointerException e) {
                        System.out.println("Error: Can not find img properties of " + imgId);
                    }
                } else {
                    System.out.println("Can not find img of " + imgId);
                }
            }
            articleDto = new SuggestModelArticleDto(article.getTitle(), article.getTags(), article.getContent(), imageDtoList);

            List<List<TagInfo>> tagsOfImages = tagSuggestor.doTag(articleDto);
            List<SuggestArticleKeywordsDto> keywordsOfArticle = tagSuggestor.doKeyword(articleDto);
            this.idsOfImages.addAll(imageIdList);
            this.tagsOfImages.addAll(tagsOfImages);
            this.keywordsOfArticle.add(keywordsOfArticle);
            if (++count % 1000 == 0) {
                DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                //get current date time with Date()
                Date date = new Date();
                System.out.print(dateFormat.format(date) + "  ");
                System.out.println(this.getName() + " finished " + count + " articles");
            }
        }
        buildResutls();
    }
}
